package cn.iocoder.yudao.module.game.controller.admin.gameProfitAndLoss;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.gameProfitAndLoss.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameProfitAndLoss.GameProfitAndLossDO;
import cn.iocoder.yudao.module.game.service.gameProfitAndLoss.GameProfitAndLossService;

@Tag(name = "管理后台 - 游戏盈亏")
@RestController
@RequestMapping("/game/profit-and-loss")
@Validated
public class GameProfitAndLossController {

    @Resource
    private GameProfitAndLossService profitAndLossService;

    @PostMapping("/create")
    @Operation(summary = "创建游戏盈亏")
    @PreAuthorize("@ss.hasPermission('game:profit-and-loss:create')")
    public CommonResult<Long> createProfitAndLoss(@Valid @RequestBody GameProfitAndLossSaveReqVO createReqVO) {
        return success(profitAndLossService.createProfitAndLoss(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新游戏盈亏")
    @PreAuthorize("@ss.hasPermission('game:profit-and-loss:update')")
    public CommonResult<Boolean> updateProfitAndLoss(@Valid @RequestBody GameProfitAndLossSaveReqVO updateReqVO) {
        profitAndLossService.updateProfitAndLoss(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除游戏盈亏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:profit-and-loss:delete')")
    public CommonResult<Boolean> deleteProfitAndLoss(@RequestParam("id") Long id) {
        profitAndLossService.deleteProfitAndLoss(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得游戏盈亏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:profit-and-loss:query')")
    public CommonResult<GameProfitAndLossRespVO> getProfitAndLoss(@RequestParam("id") Long id) {
        GameProfitAndLossDO profitAndLoss = profitAndLossService.getProfitAndLoss(id);
        return success(BeanUtils.toBean(profitAndLoss, GameProfitAndLossRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得游戏盈亏分页")
    @PreAuthorize("@ss.hasPermission('game:profit-and-loss:query')")
    public CommonResult<PageResult<GameProfitAndLossRespVO>> getProfitAndLossPage(@Valid GameProfitAndLossPageReqVO pageReqVO) {
        PageResult<GameProfitAndLossDO> pageResult = profitAndLossService.getProfitAndLossPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GameProfitAndLossRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出游戏盈亏 Excel")
    @PreAuthorize("@ss.hasPermission('game:profit-and-loss:export')")
    @OperateLog(type = EXPORT)
    public void exportProfitAndLossExcel(@Valid GameProfitAndLossPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GameProfitAndLossDO> list = profitAndLossService.getProfitAndLossPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "游戏盈亏.xls", "数据", GameProfitAndLossRespVO.class,
                        BeanUtils.toBean(list, GameProfitAndLossRespVO.class));
    }

}